package com.xzx.mybatis;

import com.xzx.mybatis.mapper.DeptMapper;
import com.xzx.mybatis.mapper.EmployeeMapper;
import com.xzx.mybatis.po.DeptPO;
import com.xzx.mybatis.po.EmployeePO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author xinzhixuan
 * @version 1.0
 * @date 2025/6/21 12:40
 */
@Slf4j
public class Main {

    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void getSqlSessionFactory() throws IOException {
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
    }


    @Test
    public void testInsert() {
        // true表示自动提交
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        EmployeePO employeePO = new EmployeePO();
        employeePO.setLastName("发财");
        employeePO.setEmail("123@qq.com");
        employeePO.setGender(1);
        mapper.insert(employeePO);
        System.out.println(employeePO);
        sqlSession.close();
    }

    @Test
    public void testParam() {
        // true表示自动提交
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        EmployeePO employeePO = mapper.getByIdAndName(3L, "发财");
        System.out.println(employeePO);
        sqlSession.close();
    }

    @Test
    public void testSelect() {
        // true表示自动提交
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        mapper.getByDiscriminator().forEach(x->{
            System.out.println(x);
            System.out.println(x.getDept());
            System.out.println("=================");
        });
        sqlSession.close();
    }
}